package controller;

import misc.Configurations;
import misc.Logger;
import data.File;
import data.DatabaseGateway;
import data.DownloadLog;
import data.IPServing;
import data.User;
import data.UploadLog;
import java.sql.*;
import java.util.LinkedList;
import java.util.ArrayList;

public class AdminController {
    public LinkedList<User> retrieveUser(){
        try{
            DatabaseGateway data = new DatabaseGateway();
            data.openConnection(Configurations.User,Configurations.Password,
                                        Configurations.DatabaseName);

            //Querying
            ResultSet rs_user = data.executeQuery("SELECT * FROM USERS");

            //Convert results to interactable items
            LinkedList<User> users = new LinkedList<User>();
            while(rs_user.next())
            {
                User new_user = new User();
                new_user.setUserName(rs_user.getString("Username"));
                new_user.setDateRegistration(rs_user.
                                                getTimestamp("RegistryDate"));
                users.add(new_user);
            }

            data.closeConnection();
            return users;
        }catch(Exception e){
            Logger.write(e.getMessage(),Logger.LogType.error);
        }

        return null;
    }

    public LinkedList<File> retrieveFile(){
        try{
            DatabaseGateway data = new DatabaseGateway();
            data.openConnection(Configurations.User,Configurations.Password,
                                        Configurations.DatabaseName);

            //Querying
            ResultSet rs_file = data.executeQuery("SELECT * FROM FILES");

            //Convert results to interactable items
            LinkedList<File> files = new LinkedList<File>();
            while(rs_file.next())
            {
                File new_file = new File();
                new_file.setFileID(rs_file.getLong("FileID"));
                new_file.setName(rs_file.getString("OriginalName"));
                new_file.setSize(rs_file.getLong("Size"));
                new_file.setFilepath(rs_file.getString("FilePath"));
                new_file.setType(rs_file.getString("Type"));
                new_file.setProtected(rs_file.getBoolean("isProtected"));
                new_file.setDeleted(rs_file.getBoolean("isDeleted"));
                new_file.setHashedPassword(rs_file.
                                            getString("HashedPassword"));
                new_file.setHashedCode(rs_file.getString("HashedCode"));
                files.add(new_file);
            }

            data.closeConnection();
            return files;
        }catch(Exception e){
            Logger.write(e.getMessage(),Logger.LogType.error);
        }

        return null;
    }

    public LinkedList<DownloadLog> retrieveDownloadLog(){
        try{
            DatabaseGateway data = new DatabaseGateway();
            data.openConnection(Configurations.User,Configurations.Password,
                                    Configurations.DatabaseName);

            //Querying
            ResultSet rs_downloads = 
                        data.executeQuery("SELECT * FROM DOWNLOADLOGS");

            //Convert results to interactable items
            LinkedList<DownloadLog> downloads = new LinkedList<DownloadLog>();
            while(rs_downloads.next())
            {
                DownloadLog new_download = new DownloadLog();
                new_download.setID(rs_downloads.getLong("ID"));
                new_download.setFileID(rs_downloads.getLong("FIleID"));
                new_download.setIPaddress(rs_downloads.getString("IP"));
                new_download.setStart(rs_downloads.
                                        getTimestamp("StartTimeDate"));
                new_download.setFinish(rs_downloads.
                                        getTimestamp("FinishTimeDate"));
                new_download.setUsername(rs_downloads.getString("Username"));
                downloads.add(new_download);
            }

            data.closeConnection();
            return downloads;
        }catch(Exception e){
            Logger.write(e.getMessage(),Logger.LogType.error);
        }

        return null;
    }

    public LinkedList<UploadLog> retrieveUploadLog(){
        try{
            DatabaseGateway data = new DatabaseGateway();
            data.openConnection(Configurations.User,Configurations.Password,
                                            Configurations.DatabaseName);

            String query="SELECT *,FILES.Size as 'filesize',"+
                "FILES.Type as'filetype',FILES.OriginalName as 'filename',"+
                "FILES.isProtected as 'password', "+
                "FILES.HashedPassword as 'hpassword' FROM UPLOADLOGS,"+
                "FILES WHERE UPLOADLOGS.FileID=FILES.FileID";
            
            //Querying
            ResultSet rs_uploads = data.executeQuery(query);

            //Convert results to interactable items
            LinkedList<UploadLog> uploads = new LinkedList<UploadLog>();
            while(rs_uploads.next())
            {
                UploadLog new_upload = new UploadLog();
                new_upload.setID(rs_uploads.getLong("ID"));
                new_upload.setFileName(rs_uploads.getString("OriginalName"));
                new_upload.setFileID(rs_uploads.getLong("FileID"));
                new_upload.setFilesize(rs_uploads.getLong("filesize"));
                new_upload.setFiletype(rs_uploads.getString("filetype"));
                new_upload.setIPaddress(rs_uploads.getString("IP"));
                new_upload.setStart(rs_uploads.getTimestamp("StartTimeDate"));
                new_upload.setFinish(rs_uploads.
                                            getTimestamp("FinishTimeDate"));
                new_upload.setUsername(rs_uploads.getString("Username"));
                new_upload.setFileIsProtected(rs_uploads.
                                            getBoolean("password"));
                new_upload.setFileHashedPassword(rs_uploads.
                                            getString("hpassword"));
                uploads.add(new_upload);
            }

            data.closeConnection();
            return uploads;
        }catch(Exception e){
            Logger.write(e.getMessage(),Logger.LogType.error);
        }

        return null;
    }

    public LinkedList<IPServing> retrieveIPServings(){
        try{
            DatabaseGateway data = new DatabaseGateway();
            data.openConnection(Configurations.User,Configurations.Password,
                                    Configurations.DatabaseName);

            //Querying
            ResultSet rs_servings = 
                            data.executeQuery("SELECT * FROM IPSERVING");

            //Convert results to interactable items
            LinkedList<IPServing> servings = new LinkedList<IPServing>();
            while(rs_servings.next())
            {
                IPServing new_serving = new IPServing();
                new_serving.setIPaddress(rs_servings.getString("IP"));
                new_serving.setDownloadingFileID(rs_servings.
                                            getLong("DownloadingFileID"));
                servings.add(new_serving);
            }

            data.closeConnection();
            return servings;
        }catch(Exception e){
            Logger.write(e.getMessage(),Logger.LogType.error);
        }

        return null;
    }
    
    public ArrayList<Long> retrievePendingFiles()
    {
        try{
            DatabaseGateway data = new DatabaseGateway();
            data.openConnection(Configurations.User,Configurations.Password,
                                    Configurations.DatabaseName);

            //Querying
            ResultSet rs =
                            data.executeQuery("SELECT * FROM PENDINGFILES");

            //Convert results to interactable items
            ArrayList<Long> list = new ArrayList<Long>();
            while(rs.next())
            {
                list.add(rs.getLong("FileId"));
            }

            data.closeConnection();
            return list;
        }catch(Exception e){
            Logger.write(e.getMessage(),Logger.LogType.error);
        }

        return null;
    
    }
}
